/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * venta.java
 *
 * Created on 27-feb-2012, 23:32:07
 */


package Ventas;

import Ventas.procsventas.classprocs;
import java.sql.SQLException;
import java.util.Calendar;
import java.sql.Date;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author pedrojesus
 */
public class venta extends javax.swing.JFrame {
classprocs procedimientos= new classprocs();
java.sql.ResultSet rs, rs2; 
int idproductoactual=0;
public ArrayList<detalleventa> detalleactual;  //contiene los detalles que se van a aingresar

public static boolean insertarncliente=true;//bandera que me indica si se va a insertar el cliente nuevo o si ya existe en la BD
    
    /** Creates new form venta */
    public venta() {
        initComponents();
        this.llenaventas();
        detalleactual = new ArrayList<detalleventa>();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButton1.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        jButton1.setText("Facturar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel6.setText("Aplicar Descuento");
        jLabel6.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jLabel6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel6MouseClicked(evt);
            }
        });

        jTextField4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField4ActionPerformed(evt);
            }
        });
        jTextField4.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField4KeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField4KeyReleased(evt);
            }
        });

        jLabel5.setText("No Nit");

        jTextField2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField2KeyPressed(evt);
            }
        });

        jTextField1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });

        jLabel3.setText("Direccion");

        jLabel2.setText("Nombre");

        jTextField3.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField3KeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField3KeyReleased(evt);
            }
        });

        jLabel4.setText("Telefono");

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Cantidad", "Nombre" ,"Codigo", "Descripcion", "Precio"
            }
        ));
        jTable2.setCellSelectionEnabled(true);
        jScrollPane2.setViewportView(jTable2);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("Ventas");

        jTextField6.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField6KeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField6KeyReleased(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(tiendaingsoftii.TiendaINGSOFTIIApp.class).getContext().getResourceMap(venta.class);
        jButton2.setIcon(resourceMap.getIcon("")); // NOI18N
        jButton2.setText("Total");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel8.setBackground(new java.awt.Color(255, 255, 255));
        jLabel8.setFont(new java.awt.Font("Lucida Grande", 1, 18));
        jLabel8.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);

        jTextField5.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField5KeyPressed(evt);
            }
        });

        jLabel7.setText("No Factura");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel7))
                        .addGap(22, 22, 22)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(233, 233, 233)
                        .addComponent(jLabel1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap(294, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 545, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 545, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
                .addContainerGap())
        );

        jButton3.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        jButton3.setIcon(resourceMap.getIcon("")); // NOI18N
        jButton3.setText("Nueva venta");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(270, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(50, 50, 50))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField1ActionPerformed

public void llenaventas(){
    
        
                    Vector tableData = new Vector();
                    Vector<String> tableHeaders = new Vector<String>();
                    
                    
                    
                    tableHeaders.add("Cantidad");
                    tableHeaders.add("Nombre");
                    tableHeaders.add("Codigo");
                    tableHeaders.add("Descripcion");
                    tableHeaders.add("Precio");
                    
                    Vector<Object> oneRow = null;
                    
                     
                             
    for(int i=0; i<Carretilla.CarretillaActual.size();i++){
        // "Cantidad", "Nombre" ,"Codigo", "Descripcion", "Precio"
            oneRow = new Vector<Object>();
                        oneRow.add(1);//cantidad            
                        oneRow.add(Carretilla.CarretillaActual.get(i).getNombre());
                        oneRow.add(Carretilla.CarretillaActual.get(i).getCodigo());
                        oneRow.add(Carretilla.CarretillaActual.get(i).getDescripcion());
                        oneRow.add(Carretilla.CarretillaActual.get(i).getPrecioventa());
                        
                        
                        tableData.add(oneRow);
                     
    
    }
                    
                    
                    jTable2.setModel(new MiModelo(tableData, tableHeaders));
               
        
   
    
    
    }





private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
/////////////////////////////---------------PROCEDIMIENTO QUE SIRVE PARA VER SI SINSERTAMOS O NO EL CLIENTE QUE ACABAMOS DE ESCRIBIR\\\\\\\\\\\\
            
    try {
            String NitCliente="";
            
            rs=procedimientos.Verclienteslike(jTextField4.getText());

            while(rs.next()){
            NitCliente=String.valueOf(rs.getObject(1));
            
            
          if(NitCliente.compareTo(jTextField4.getText())==0){//si encuentra un nit igual en la base de datos
          insertarncliente=false;   //entonces ya no lo insrtamos en la tabla clientes
          break;
          }
            
            
            }
            
                        JOptionPane.showMessageDialog(null, "el nit es"+NitCliente);

            
        } catch (SQLException ex) {
            Logger.getLogger(venta.class.getName()).log(Level.SEVERE, null, ex);
        }

if(insertarncliente==true){
            try {
                procedimientos.insertarcliente(jTextField4.getText(), jTextField1.getText(), jTextField2.getText(), jTextField3.getText());
                
            } catch (SQLException ex) {
                Logger.getLogger(venta.class.getName()).log(Level.SEVERE, null, ex);
            }
}else{

JOptionPane.showMessageDialog(null, "nop se ingresa el cliente porque ya existe en la tabla gay");

insertarncliente=true;
            
}
    
/////////////////////////    FIN DEL PROCEDIMIENTO\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//
//
////////////////////////     INGRESO EN TABLA FACTURA VENTA ////////////////////////////////////////    
//los campos necesarios para insertar son     
//NoFACTURA, FECHA, TOTAL, DESCUENTO, NITCLIENTE
//CAMPO FECHA
String NoFactura="";

double total=0;
int descuento=0;
String nitcliente="";
int diaconv=0;
String diaconv1="";
NoFactura=jTextField5.getText(); 

Date fecha = null;
Calendar c1 = Calendar.getInstance();
Calendar c2 = new GregorianCalendar();
String dia = Integer.toString(c2.get(Calendar.DATE));
String mes =Integer.toString(c2.get(Calendar.MONTH)+1);
String anio = Integer.toString(c2.get(Calendar.YEAR));
diaconv=Integer.parseInt(dia);
if(diaconv<10){
   diaconv1="0"+dia;    
}
else{
    diaconv1=String.valueOf(diaconv);
    }

if(Integer.parseInt(mes)<10){
   mes="0"+mes;    
}
else{
    mes=String.valueOf(mes);
    }



String fechacad=anio+"-"+mes+"-"+diaconv1;   
JOptionPane.showMessageDialog(null, "la fecha es"+fechacad);

Date fechaconv;
fechaconv=Date.valueOf(fechacad);

if(jLabel8.getText().compareTo("")==0){
JOptionPane.showMessageDialog(null, "No ha sacado el total de la venta efectuada");
}else{
total=Double.valueOf(jLabel8.getText());

}

if(jTextField6.getText().compareTo("")!=0){
descuento=Integer.parseInt(jTextField6.getText());
}else{
    
descuento=0;
JOptionPane.showMessageDialog(null, "no se aplico ningun descuento a los productos");
}

nitcliente=jTextField4.getText();
if(NoFactura.compareTo("")!=0){   //si ingresaron un numero para la factura

    
    
    try {
            procedimientos.insertarfactura(NoFactura, fechaconv, total, descuento, nitcliente);

    
/////////////////////--------------INGRESO EN EL ARRAYLIST DETALLE DE VENTA----------///////////////
//los campos son Cantidad, Precio, idproducto, No Factura
  
    for(int i=0; i<jTable2.getRowCount();i++){
    
    int cantidad=Integer.parseInt(jTable2.getValueAt(i, 0).toString());
    Double precio=Double.valueOf(jTable2.getValueAt(i, 4).toString()); //
    
    try {
            rs=procedimientos.Verproductosprcodigo(String.valueOf(jTable2.getValueAt(i, 2)));
   
            
             while(rs.next()){
                 
    if(jTable2.getValueAt(i, 2).toString().compareTo(rs.getObject(1).toString())==0){
    
        idproductoactual=Integer.parseInt(rs.getObject(8).toString());
    
    }
    
    }
             
    JOptionPane.showMessageDialog(null, "producto actual es"+idproductoactual);
             
            
                 procedimientos.insertardetalleventa(cantidad, precio, idproductoactual, NoFactura);  //inserta los detalles de las ventas
           
                 procedimientos.BorraExistPro(idproductoactual, cantidad);//le resta el valor a el producto seleccionado     
     
                 JOptionPane.showMessageDialog(null, "Vendido Articulo"+jTable2.getValueAt(i, 1));
            
            
             
             
            } catch (SQLException ex) {
                Logger.getLogger(venta.class.getName()).log(Level.SEVERE, null, ex);
            }

   
    
    }
  

////////////////////---------------FIN INGRESO EN LA TABLA DETALLE DE VEMTA-------//////////////

////////////////////---------------BORRADO DE EXISTENCIA DE LOS PRODUCTOS QUE ACABAMOS DE VENDER---------////////////       
    
    
////////////////////---------------FIN DE BORRADO DE EXISTENCIA DE LOS PRODUCTOS QUE ACABAMOS DE VENDER---------////////////

    
    JOptionPane.showMessageDialog(null, "Venta Realizada");
    
    
    
    } catch (SQLException ex) {
            Logger.getLogger(venta.class.getName()).log(Level.SEVERE, null, ex);
        }
}else{

JOptionPane.showMessageDialog(null, "No se Puede Ingresar una Venta sin el Numero de Factura");
}
//////////////////////-------------FIN INGRESO EN LA TABAL FACTURA----////////////////


}//GEN-LAST:event_jButton1ActionPerformed

private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField4ActionPerformed
        
    try {
            String NitCliente="";
            String Nombre="";
            String Direccion="";
            String Telefono="";

            rs=procedimientos.Verclienteslike(jTextField4.getText());

            while(rs.next()){
            NitCliente=String.valueOf(rs.getObject(1));
            Nombre=String.valueOf(rs.getObject(2));
            Direccion=String.valueOf(rs.getObject(3));
            Telefono=String.valueOf(rs.getObject(4));
            
            jTextField1.setText(Nombre);
            jTextField2.setText(Direccion);
            jTextField3.setText(Telefono);
            jTextField4.setText(NitCliente);
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(venta.class.getName()).log(Level.SEVERE, null, ex);
        }



}//GEN-LAST:event_jTextField4ActionPerformed

private void jTextField4KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField4KeyPressed
if(jTextField4.getText().length()>14){
    String nit="";
    nit=jTextField4.getText().substring(0, 14);
    
    jTextField4.setText("");
    
    jTextField4.setText(nit);
JOptionPane.showMessageDialog(null, "el limite es de 15 caracteres para el ingreso del No de Nit");
}

}//GEN-LAST:event_jTextField4KeyPressed

private void jLabel6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel6MouseClicked


  if((jTable2.getSelectedRow() >= 0)){//si no se hace la busqueda
    if ((jTable2.getValueAt(jTable2.getSelectedRow(),4) !=null)&&(jTextField6.getText().compareTo("")!=0)){//a nivel de objetos se compara si es nula la casilla
        

    float preciooriginal=0;
   float preciocondescuento=0;
   float descuento=0;
   descuento=Float.valueOf(jTextField6.getText());
   descuento=descuento/100;
    preciooriginal= Float.valueOf(String.valueOf(jTable2.getValueAt(jTable2.getSelectedRow(), 4)));
   descuento=preciooriginal*descuento;
   preciocondescuento=preciooriginal-descuento;
   jTable2.setValueAt(preciocondescuento,jTable2.getSelectedRow(), 4);
        
        
    JOptionPane.showMessageDialog(null, "Descuento Aplicado");

}else{
    JOptionPane.showMessageDialog(null, "Debe Ingresar el monto del Descuento Previamente");
    
    }
        
        
        }else{
        
         JOptionPane.showMessageDialog(null, "Debe Selecionar un producto para Poder Aplicar el des");
   
        } 
    

}//GEN-LAST:event_jLabel6MouseClicked

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

    float total=0;
float numitems=0;
    for(int i=0; i<jTable2.getRowCount();i++){
        numitems=Float.valueOf(String.valueOf(jTable2.getValueAt(i, 0)));
    total+=Float.valueOf(String.valueOf(jTable2.getValueAt(i, 4)))*(numitems);
    }
    
    jLabel8.setText(String.valueOf(total));
    // TODO add your handling code here:
}//GEN-LAST:event_jButton2ActionPerformed

private void jTextField4KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField4KeyReleased
    // TODO add your handling code here:
}//GEN-LAST:event_jTextField4KeyReleased

private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
    if(jTextField1.getText().length()>59){
    String nit="";
    nit=jTextField1.getText().substring(0, 59);
    
    jTextField1.setText("");
    
    jTextField1.setText(nit);
JOptionPane.showMessageDialog(null, "el limite es de 60 caracteres para el ingreso del Nombre del Cliente");
    }
}//GEN-LAST:event_jTextField1KeyPressed

private void jTextField2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField2KeyPressed

    if(jTextField2.getText().length()>59){
    String nit="";
    nit=jTextField2.getText().substring(0, 59);
    
    jTextField2.setText("");
    
    jTextField2.setText(nit);
JOptionPane.showMessageDialog(null, "el limite es de 60 caracteres para el ingreso de la direccion del Cliente");
    }


}//GEN-LAST:event_jTextField2KeyPressed

private void jTextField3KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField3KeyReleased
    // TODO add your handling code here:
}//GEN-LAST:event_jTextField3KeyReleased

private void jTextField3KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField3KeyPressed
    if(jTextField3.getText().length()>7){
    String nit="";
    nit=jTextField3.getText().substring(0, 7);
    
    jTextField3.setText("");
    
    jTextField3.setText(nit);
JOptionPane.showMessageDialog(null, "el limite es de 8 caracteres para el ingreso del # Telefonico del Cliente");
    }

}//GEN-LAST:event_jTextField3KeyPressed

private void jTextField5KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField5KeyPressed

    if(jTextField5.getText().length()>14){
    String nit="";
    nit=jTextField5.getText().substring(0, 14);
    
    jTextField5.setText("");
    
    jTextField5.setText(nit);
JOptionPane.showMessageDialog(null, "el limite es de 15 caracteres para el ingreso de la factura");
    }

}//GEN-LAST:event_jTextField5KeyPressed

private void jTextField6KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField6KeyPressed

 //   if(this.isNumeric(String.valueOf(evt.getKeyChar()))==false){
 //   jTextField6.setText("");
    
    /*
    String nit="";
    if(jTextField6.getText().length()>1){
    nit=jTextField6.getText().substring(0, jTextField6.getText().length()-1);
    
    jTextField6.setText("");
    jTextField6.setText(nit);
JOptionPane.showMessageDialog(null, "Debe Ingresar Solamente Numeros");

    }
    else{
        
    jTextField6.setText("");
JOptionPane.showMessageDialog(null, "Debe Ingresar Solamente Numeros");

    }*/
//}


     
}//GEN-LAST:event_jTextField6KeyPressed

private void jTextField6KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField6KeyReleased
   if((this.isNumeric(String.valueOf(evt.getKeyChar()))==false)&&(evt.getKeyCode()!=10)&&((evt.getKeyCode()!=37))&&(evt.getKeyCode()!=38)&&((evt.getKeyCode()!=39)&&(evt.getKeyCode()!=40))){

    String nit="";
    if(jTextField6.getText().length()>1){
    nit=jTextField6.getText().substring(0, jTextField6.getText().length()-1);
    
    jTextField6.setText("");
    jTextField6.setText(nit);
JOptionPane.showMessageDialog(null, "Debe Ingresar Solamente Numeros");

    }
    else{
        
    jTextField6.setText("");
JOptionPane.showMessageDialog(null, "Debe Ingresar Solamente Numeros");

    }
}

}//GEN-LAST:event_jTextField6KeyReleased

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

    Carretilla.CarretillaActual.clear();
    this.dispose();
Carretilla micarreta=new Carretilla();
micarreta.setVisible(true);
   JOptionPane.showMessageDialog(null, "el tam,año es"+Carretilla.CarretillaActual.size());
    
    // TODO add your handling code here:
}//GEN-LAST:event_jButton3ActionPerformed

private static boolean isNumeric(String cadena){
	try {
		Integer.parseInt(cadena);
		return true;
	} catch (NumberFormatException nfe){
		return false;
	}
}
    

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(venta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(venta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(venta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(venta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new venta().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    public static javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    // End of variables declaration//GEN-END:variables
}
